<{include file="top.html" }>

<style>
    div.hint{top:35%;}
</style>

</head>
<body>
<header><a class="left_link" href="javascript:void(0);"></a>活动报名</header>
<h2 class="title"><img src="<{$preUploadcUrl}><{$info.imgList}>"><span><{$info.typeHtml}> | <{$info.title}></span></h2>
<h3 class="topic-name">报名信息</h3>
<ul class="zhbd_box name_box">
    <li class="zhbd_list_1"><span>姓名：</span><input type="text" maxlength="6" id="realname" placeholder="请填写真实姓名" value="<{$userinfo.realName}>" /></li>
    <{if $userinfo.bindPhone}>
        <li class="zhbd_list_1 zhbd_list_2"><span>手机号：</span><input type="tel" id="phone" maxlength="11" placeholder="请填写联系人手机号" value="<{$userinfo.bindPhone}>" /></li>
    <{else}>
        <li class="zhbd_list_1 zhbd_list_2"><span>手机号：</span><input type="tel" style="width:40%" id="phone" maxlength="11" placeholder="请填写联系人手机号" /><a class="hqyz" href="javascript: void(0);">获取验证码</a></li>
        <li class="zhbd_list_1"><span>验证码：</span><input id="code" type="number" maxlength="6" placeholder="请输入验证码" /></li>
    <{/if}>
</ul>
<h3 class="topic-name" style="padding-bottom: 1rem;">留言</h3>
<div class="msg">
    <textarea id="mark" name="" rows="" cols="" placeholder="留言会提高入选率这件事，<{$selfName}>会乱说？"></textarea>
</div>
<dl class="ly_con">留言将在活动页展示</dl>
<{if $info.point || ($info.price*100)>0}>
<h3 class="topic-name" style="padding-bottom: 1rem;">支付信息</h3>
<div class="pay-list">
    <{if $info.point}><dl class="list">所需积分（拥有<{$userinfo.point}>积分）<span><{$info.point}>积分</span></dl><{/if}>
    <{if ($info.price*100)>0}><dl class="list">所需金额<span><{$info.price}>元</span></dl><{/if}>
</div>
<{/if}>

<div class="ui-button ui-button-2">
    <button id="apply">提交报名</button>
</div>

<!--弹出框 mustFollow-->
<div id="mustFollow" class="hint-box" style="display:none;">
    <div class="hint nofollow_box">
        <div class="topic title">请关注丸子妈妈！</div>
        <{if !$userinfo.WX_openid}>
        <dl class="con"><img src="<{$preStaticUrl}>Wap/images/<{$qrcode_path}>" /></dl>
        <{/if}>
        <dl class="list text">长按二维码关注</dl>
        <dl class="list shibie">享受全部福利</dl>
        <button type="button" class="button zdl">知道了</button>
    </div>
</div>


<!--弹出框 成功提示-->
<div id="applySuccess" class="hint-box" style="display:none;">
    <div class="hint nofollow_box">
        <div class="topic title">报名成功！</div>
        <{if !$userinfo.WX_openid}>
         <dl class="con"><img src="<{$preStaticUrl}>Wap/images/<{$qrcode_path}>" /></dl>
        <{/if}>
        <dl class="list text">更多福利，请继续关注我们哦</dl>
        <{if !$userinfo.WX_openid}>
            <dl class="list shibie">长按二维码关注</dl>
        <{/if}>
        <button type="button" class="button zdl">知道了</button>
    </div>
</div>

<!--弹出框秒抢—积分兑换-->
<div id="applySuccessItem" class="hint-box" style="display:none;">
    <div class="hint welfare_box">
        <a class="close zdl" href="javascript:void(0);"></a>
        <div class="topic">很遗憾未中奖</div>
        <h3>专属福利</h3>
        <dl class="con"><img id="itemimg" src="<{$preStaticUrl}>Wap/images/dy_img.jpg" /></dl>
        <dl class="list">商品名称：<font id="itemtitle">联盛中影50元代金券</font></dl>
        <dl class="list">商品价格：<font id="itempoint">5000积分</font></dl>
        <dl class="list">专属价格：<font id="salePoint">4000积分（8折）</font></dl>

        <button  type="button" class="button buy">立刻购买</button>
    </div>
</div>

<!--弹出框 报名错误提示-->
<div id="applyError" class="hint-box" style="display:none;">
    <div class="hint nofollow_box">
        <div class="topic">提示</div>
        <dl class="list">报名结果将第一时间在XXX公布</dl>
        <button type="button" class="button zdl">知道了</button>
    </div>
</div>
<!-- 积分商城信息补足 -->
<div class="hint-box" id="pointMallForm">
    <div class="hint hdbm_box">
        <div class="topic" id="send-title">请完善您的信息</div>
        <div class="title">请填写信息，工作人员将尽快为您发货。<br>信息不完整视为自动放弃哦~</div>
          <div class="hdbm_item" style="margin-top:0;">
            姓&nbsp;&nbsp;名
            <input type="text" id="realName" value="<{$userinfo.realName}>"/>
        </div>
        <div class="hdbm_item">
            联系电话
            <input type="text"  id="phoneNum" value="<{$userinfo.bindPhone}>"/>
        </div>
        <div class="hdbm_item jfdh_form">
            详细地址
            <textarea id="address"><{$userinfo.address}></textarea>
        </div>
        <button type="button" id="send-btn-exchange" onclick="deliverExchange()" class="button">提交信息</button>
    </div>
</div>
<{include file="signBody.html"}>

</body>
</html>

<script type="text/javascript" src="<{$preStaticUrl}>js/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="<{$preStaticUrl}>js/jquery.touchSlider.js"></script>
<script type="text/javascript">
    var joinUrl = "<{U('Events/join')}>";
    var itemUrl = "<{U('PointMall/info')}>";
    var smsUrl = "./api.php?m=api&c=Sms&a=sendSmsCode";
    var id = "<{$info.id}>";
    var comeback = 0;
    var itemid = 0;
    var deliveryType=0;

    $(function(){

        $("#apply").on('click', function () {
            var realname = $("#realname").val()
                    ,phone = $("#phone").val()
                    ,mark = $("#mark").val()
                    ,code = $("#code").val();
            if (!validatePhone(phone)) {
                $("#applyError .list").html('请输入正确的手机号码');
                $("#applyError").show();
                return false;
            };
            loadObj = layer.load(2);
            var param = {realname:realname,phone:phone,id:id,mark:mark,code:code};
            $.getJSON(joinUrl, param, function(data){
                layer.close(loadObj);
                comeback = data.comeback;
                if(data.status!=0){
                    switch(data.status){
                        case 2://关注
                            $('#mustFollow').show();
                            break;
                        default:
                            $("#applyError .list").html(data.msg);
                            $("#applyError").show();
                            break;
                    }

                }else{
                    if(data.payUrl.length>0){
                        window.location.href=data.payUrl;
                        return false;
                    }
                    if(typeof(data.item) != undefined && data.item.length<=0){

                        if(data.hasFollow){
                            layer.msg("活动报名成功",{time:1500},function(){
                                location.href="<{u('Events/info?id='|cat:$info.id)}>";
                            });
                        }else{//未关注提示关注
                            if (data.title.length > 0) $("#applySuccess .title").html(data.title);
                            if (data.text.length > 0) $("#applySuccess .text").html(data.text);
                            $("#applySuccess").show();
                        }
                    }else{
                        $("#applySuccessItem #itemimg").attr('src',data.item.img);
                        $("#applySuccessItem #itemtitle").html(data.item.title);
                        $("#applySuccessItem #itempoint").html(data.item.point+"积分");
                        $("#applySuccessItem #salePoint").html(data.item.salePoint+"积分("+data.item.discount+"折)");
                        $("#applySuccessItem").show();
                        itemid = data.item.id;
                        deliveryType=data.item.deliveryType;
                    }
                }
            });
        });

        $(".hqyz").on('click', function () {
            var phone = $("#phone").val();
            if (!validatePhone(phone)) {
                $("#applyError .list").html('请输入正确的手机号码');
                $("#applyError").show();
                return false;
            };
            if (!flag) return false;
            $.getJSON(smsUrl, {phone:phone,bind:1}, function(data){
                    $("#applyError .list").html(data.msg);
                    $("#applyError").show();

                    flag = false;
                    $('.hqyz').html('<font>60</font>秒后重新获取');
                    timer = setInterval(countDown,1000);
            });
        });

        $(".button.zdl, .close.zdl").click(function() {
            $(".hint-box").css({"display":"none"});
            if(comeback == 1){
                window.history.back();
                return false;
            }
        });
        $(".button.buy").click(function() {
            $(".hint-box").css({"display":"none"});
            doExchange();
        });

        var timer;
        var flag = true;
        var countDown = function CountDown() {
            var maxtime = parseInt($(".hqyz font").html());
            if(maxtime>=1){
                --maxtime;
                $('.hqyz font').html(maxtime);
            }else{
                clearInterval(timer);
                $('.hqyz').html('获取验证码');
                flag = true;
            }
        }

    });



    //普通兑换 事件总入口
    function doExchange(){
    <{if !$userinfo}>
        location.href="<{U('User/login')}>";
        return;
    <{/if}>

        //需要发货
        const DELIVERY_SEND = <{$DELIVERY_SEND}>;
        //卡券
        const DELIVERY_CARD = <{$DELIVERY_CARD}>;

        $.ajax({
            url:"<{U('PointMall/validateExchange')}>",
            dataType:"json",
            data:{"id":itemid,"eventID":id},
            type:"GET",
            success:function(data){
                if(data.status == 0){
                    if(deliveryType == DELIVERY_SEND){
                        showDeliverExchangeForm();
                    }else if(deliveryType == DELIVERY_CARD){
                        $.ajax({
                            url:"<{U('PointMall/cardExchange')}>",
                            dataType:"json",
                            data:{"id":itemid,'eventID':id},
                            type:"GET",
                            success:function(data){
                                if(data.status == 0){
                                    layer.closeAll();
                                    layer.msg(data.msg);
                                    goUserCard();
                                }else{
                                    layer.msg(data.msg);
                                }
                            },
                        });
                    }
                }else{
                    layer.msg(data.msg);
                }
            },

        });

    };

    //显示普通兑换-配送表单
    function showDeliverExchangeForm(){
        $("#pointMallForm").show();
    }

    //配送--普通兑换
    function deliverExchange(){
        var phone   = $('#phoneNum').val();
        var name    = $('#realName').val();
        var address = $('#address').val();
        $.ajax({
            url:"<{U('PointMall/doExchange')}>",
            dataType:"json",
            data:{"id":itemid,"eventID":id,"realname":name,"address":address,"phone":phone},
            type:"POST",
            success:function(data){
                if (data.status==0) {
                    $('#pointMallForm').hide();
                    layer.msg(data.msg);
                }else{
                    layer.msg(data.msg);
                }
            },
            error:function(data){
                layer.msg('异常');
            }
        });
    };
</script>
